package com.mq.produce.simple;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.nio.charset.Charset;
import java.util.Map;

/**
 * @Classname MyPartitioner
 * @Description
 * @Version 1.0.0
 * @Date 2024/12/12 14:42
 * @Created by ncs
 */
public class MyPartitioner implements Partitioner {

    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        int size = cluster.partitionsForTopic(topic).size();
        int hashCode = key.hashCode();
        int partition = hashCode % size;
        System.out.println(key+"的hash=" + hashCode +"分区数据到partition="+partition);
        return partition;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
